package com.meritdata.cloud.data.manager.model.controller;


import com.meritdata.cloud.data.manager.model.entity.TdTempModel;
import com.meritdata.cloud.data.manager.model.service.IDataModelColumnService;
import com.meritdata.cloud.data.manager.model.service.impl.DataModelColumnService;
import com.meritdata.cloud.resultmodel.ResultBody;
import com.meritdata.cloud.resultmodel.ResultStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.Date;

@RestController
@RequestMapping("/api/manager/modelcolumn")
@Api(value = "物理模型字段信息-DataModelColumnController", tags = "物理模型字段信息")
public class DataModelColumnController {

    @Autowired
    private IDataModelColumnService dataModelColumnService;

    @RequestMapping("/getColumnInfoData")
    @ApiOperation(value = "模型字段信息查询接口", notes = "根据模型id获取模型所有字段信息")
    public ResultBody getColumnInfoData(@ApiParam(value = "模型id") @RequestParam Long tableId) {

        try {
            return ResultBody.success(dataModelColumnService.getColumnInfoData(tableId));
        } catch (RuntimeException e) {
            return ResultBody.failure(ResultStatus.DATA_OPERATION_ERROR, e);
        }
    }

    @RequestMapping("/qualityRuleList")
    @ApiOperation(value = "质量规则查询接口", notes = "获取数据质量规则信息")
    public ResultBody qualityRuleList() {
        try {
            return ResultBody.success(dataModelColumnService.qualityRuleList());
        } catch (RuntimeException e) {
            return ResultBody.failure(ResultStatus.DATA_OPERATION_ERROR, e);
        }
    }

    @RequestMapping("/saveQualityRule")
    @ApiOperation(value = "列质量规则保存接口", notes = "保存列质量规则规则信息")
    public ResultBody saveQualityRule( @ApiParam(value = "物理表id", required = true) @RequestParam long tableId,
                                      @ApiParam(value = "列字段id", required = true) @RequestParam long columnId,
                                       @ApiParam(value = "字段名称", required = true) @RequestParam String columnName,
                                      @ApiParam(value = "质量规则Id", required = false) @RequestParam long qualityRuleId,
                                      @ApiParam(value = "码表id", required = false) @RequestParam long dictPartId,
                                      @ApiParam(value = "码表字段id" ,required = false) @RequestParam long dictPartColumnId) {
        try {
            return ResultBody.success(dataModelColumnService.saveQualityRule(tableId,columnId,columnName,qualityRuleId,dictPartId,dictPartColumnId));
        } catch (RuntimeException e) {
            return ResultBody.failure(ResultStatus.DATA_OPERATION_ERROR, e);
        }
    }

    @RequestMapping("/multiSaveQualityRule")
    @ApiOperation(value = "列质量规则批量保存接口", notes = "批量保存列质量规则规则信息")
    public ResultBody multiSaveQualityRule( @ApiParam(value = "物理表id", required = true) @RequestParam long tableId,
                                       @ApiParam(value = "列字段ids", required = true) @RequestParam long[] columnIds,
                                       @ApiParam(value = "字段名称数组", required = true) @RequestParam String[] columnNames,
                                       @ApiParam(value = "质量规则Id", required = false) @RequestParam long qualityRuleId,
                                       @ApiParam(value = "码表id", required = false) @RequestParam long dictPartId,
                                       @ApiParam(value = "码表字段id" ,required = false) @RequestParam long dictPartColumnId) {
        try {
            return ResultBody.success(dataModelColumnService.multiSaveQualityRule(tableId,columnIds,columnNames,qualityRuleId,dictPartId,dictPartColumnId));
        } catch (RuntimeException e) {
            return ResultBody.failure(ResultStatus.DATA_OPERATION_ERROR, e);
        }
    }



    @RequestMapping("/saveMaskRule")
    @ApiOperation(value = "列脱敏规则保存接口", notes = "保存列脱敏规则规则信息")
    public ResultBody saveMaskRule( @ApiParam(value = "物理表id", required = true) @RequestParam long tableId,
                                    @ApiParam(value = "字段名称", required = true) @RequestParam String columnName,
                                    @ApiParam(value = "脱敏规则Id", required = true) @RequestParam long datamaskId,
                                    @ApiParam(value = "密级", required = true) @RequestParam int securityLevelCode) {
        try {
            return ResultBody.success(dataModelColumnService.saveMaskRule(tableId,columnName,datamaskId,securityLevelCode));
        } catch (RuntimeException e) {
            return ResultBody.failure(ResultStatus.DATA_OPERATION_ERROR, e);
        }
    }

    @RequestMapping("/multiSaveMaskRule")
    @ApiOperation(value = "列脱敏规则批量保存接口", notes = "批量保存列脱敏规则规则信息")
    public ResultBody multiSaveMaskRule( @ApiParam(value = "物理表id", required = true) @RequestParam long tableId,
                                    @ApiParam(value = "字段名称", required = true) @RequestParam String[] columnNames,
                                    @ApiParam(value = "脱敏规则Id", required = true) @RequestParam long datamaskId,
                                    @ApiParam(value = "密级", required = true) @RequestParam int securityLevelCode) {
        try {
            return ResultBody.success(dataModelColumnService.multiSaveMaskRule(tableId,columnNames,datamaskId,securityLevelCode));
        } catch (RuntimeException e) {
            return ResultBody.failure(ResultStatus.DATA_OPERATION_ERROR, e);
        }
    }

    @RequestMapping("/delQualityRule")
    @ApiOperation(value = "质量规则删除接口", notes = "根据Id删除列数据质量规则信息")
    public ResultBody delQualityRule(@ApiParam(value = "质量规则配置表id", required = true) @RequestParam long qualityRuleColumnConfigId) {
        try {
            return ResultBody.success(dataModelColumnService.delQualityRule(qualityRuleColumnConfigId));
        } catch (RuntimeException e) {
            return ResultBody.failure(ResultStatus.DATA_OPERATION_ERROR, e);
        }
    }
    @RequestMapping("/delMaskRule")
    @ApiOperation(value = "脱敏规则删除接口", notes = "根据列字段名称删除列数据脱敏规则信息")
    public ResultBody delMaskRule(@ApiParam(value = "列置表id", required = true) @RequestParam long columnConfigId) {
        try {
            return ResultBody.success(dataModelColumnService.delMaskRule(columnConfigId));
        } catch (RuntimeException e) {
            return ResultBody.failure(ResultStatus.DATA_OPERATION_ERROR, e);
        }
    }
    @RequestMapping("/getModelDataDetail")
    public ResultBody getModelDataDetail(@ApiParam(value = "模型表id", required = true) @RequestParam long tableId) {
        try {
            ArrayList list = new ArrayList();

            TdTempModel  temp =new TdTempModel(1L,"colum1",1,new Date());
            TdTempModel  temp1 =new TdTempModel(2L,"colum2",2,new Date());
            TdTempModel  temp2 =new TdTempModel(3L,"colum3",3,new Date());
            TdTempModel  temp3 =new TdTempModel(4L,"colum4",4,new Date());
            list.add(temp);list.add(temp1);list.add(temp2);list.add(temp3);
            return ResultBody.success(list);
        } catch (RuntimeException e) {
            return ResultBody.failure(ResultStatus.DATA_OPERATION_ERROR, e);
        }
    }

    @RequestMapping("/getModelDataAnalyse")
    public ResultBody getModelDataAnalyse(@ApiParam(value = "模型表id", required = true) @RequestParam long tableId) {
        try {
        String reslut = "{\n" +
                "                \"dataSize\": 4602000,\n" +
                "                    \"columns\": [\n" +
                "            {\n" +
                "                 \"column\": \"column1\",\n" +
                "                    \"nullCount\": 0,\n" +
                "                    \"mean\": 1.1986666666666301,\n" +
                "                    \"maximum\": 2.5,\n" +
                "                    \"minimum\": 0.1,\n" +
                "                    \"sum\": 5516263.999999832,\n" +
                "                    \"stdDev\": 0.7606126185881766,\n" +
                "                    \"percentiles\": \"[0.3,1.3,1.8,2.5]\",\n" +
                "                    \"histogram\": [\n" +
                "                {\n" +
                "                    \"low_value\": 0.1,\n" +
                "                        \"high_value\": 0.5,\n" +
                "                        \"count\": 1442048\n" +
                "                }\n" +
                "        \n" +
                "      ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"column\": \"column2\",\n" +
                "                    \"nullCount\": 0,\n" +
                "                    \"histogram\": [\n" +
                "                {\n" +
                "                    \"value\": \"Iris-virginica\",\n" +
                "                        \"count\": 1534000,\n" +
                "                        \"ratio\": 0.3333333333333333\n" +
                "                }\n" +
                "        \n" +
                "      ]\n" +
                "            }\n" +
                "  ]\n" +
                "}";
            return ResultBody.success(JSON.parseObject(reslut));
        } catch (RuntimeException e) {
            return ResultBody.failure(ResultStatus.DATA_OPERATION_ERROR, e);
        }
    }
}
